package 普通数组;

/**
 * @author tmh
 * @date 2024/11/27 20:03
 * @description
 */
public class T189轮转数组 {
    public void rotate(int[] nums, int k) {
        //计算往右轮转多少次
        k = k % nums.length;
        //先翻转整个数组
        reverse(nums, 0, nums.length - 1);
        //翻转前面k个数
        reverse(nums, 0, k - 1);
        //翻转后面nums.length-k数
        reverse(nums, k, nums.length - 1);
    }

    //翻转算法
    public void reverse(int[] nums, int start, int end) {
        for (; start < end; start++, end--) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
        }
    }
}
